package com.lyouu.dataminer.xmlhandler;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import com.lyouu.dataminer.model.TourArea;
import com.lyouu.dataminer.model.TourCity;
import com.lyouu.dataminer.model.TourCountry;
import com.lyouu.dataminer.model.TourViewpoint;
import com.lyouu.dataminer.util.Constants;

public class TourMapXmlParser {
	private static Logger logger = Logger.getLogger(TourMapXmlParser.class);

	public static void parse(ArrayList<TourCountry> countryList,
			ArrayList<TourCity> cityList) {
		SAXBuilder builder = new SAXBuilder();
		File xmlFile = new File(System.getProperty("user.dir")
				+ "/src/tour_city.xml");
		try {
			Document document = (Document) builder.build(xmlFile);
			Element rootNode = document.getRootElement();
			List<Element> areas = rootNode.getChildren();
			for (int i = 0; i < areas.size(); i++) {
				Element areaNode = areas.get(i);
				TourArea area = new TourArea(
						areaNode.getAttributeValue(Constants.ATT_NAME_CN),
						areaNode.getAttributeValue(Constants.ATT_NAME_EN));
				List<Element> countries = areaNode.getChildren();
				for (int j = 0; j < countries.size(); j++) {
					Element countryNode = countries.get(j);
					TourCountry country = new TourCountry(
							area,
							countryNode
									.getAttributeValue(Constants.ATT_NAME_CN),
							countryNode
									.getAttributeValue(Constants.ATT_NAME_EN),
							countryNode
									.getAttributeValue(Constants.ATT_NAME_ORIG));
					countryList.add(country);
					List<Element> cities = countryNode.getChildren();
					for (int k = 0; k < cities.size(); k++) {
						Element cityNode = cities.get(k);
						List<Element> viewpoints = cityNode.getChildren();
						ArrayList<TourViewpoint> viewpointList = new ArrayList<TourViewpoint>();
						for (int a = 0; a < viewpoints.size(); a++) {
							viewpointList.add(new TourViewpoint(viewpoints.get(
									a).getAttributeValue(
									Constants.ATT_NAME_CN), viewpoints
									.get(a).getAttributeValue(
											Constants.ATT_NAME_EN),
									viewpoints.get(a).getAttributeValue(
											Constants.ATT_NAME_ORIG)));
						}
						cityList.add(new TourCity(
								country,
								cityNode.getAttributeValue(Constants.ATT_NAME_CN),
								cityNode.getAttributeValue(Constants.ATT_NAME_EN),
								cityNode.getAttributeValue(Constants.ATT_NAME_ORIG),
								viewpointList));
					}
				}
			}
		} catch (IOException io) {
			logger.info(io.getMessage());
		} catch (JDOMException jdomex) {
			logger.info(jdomex.getMessage());
		}
	}

	public static void main(String[] args) {
		ArrayList<TourCountry> countryList = new ArrayList<TourCountry>();
		ArrayList<TourCity> cityList = new ArrayList<TourCity>();
		TourMapXmlParser.parse(countryList, cityList);
		for (TourCountry c : countryList) {
			logger.info("Area:\t" + c.getArea().getNameCn() + "\t"
					+ c.getArea().getNameEn() + "\t" + "Country:\t"
					+ c.getNameCn() + "\t" + c.getNameEn() + "\t"
					+ c.getNameOrig());
		}
		logger.info("\n");
		for (TourCity c : cityList) {
			logger.info("Country:\t" + c.getCountry().getNameCn() + "\t"
					+ c.getCountry().getNameEn() + "\t"
					+ c.getCountry().getNameOrig() + "\t" + c.getNameCn()
					+ "\t" + c.getNameEn() + "\t" + c.getNameOrig());
			for (int i = 0; i < c.getViewpoints().size(); i++)
				logger.info("Viewpoint " + (i + 1) + ":\t"
						+ c.getViewpoints().get(i).getNameEn() + "\t"
						+ c.getViewpoints().get(i).getNameOrig() + "\t"
						+ c.getViewpoints().get(i).getNameCn());
		}
	}
}
